from ttzScrapyProject.utils import MumberUtils

"""
写入内容
"""


def writeCountDownFuck(fileName, countDownFuck):
    try:
        with open(fileName, "w", encoding="utf-8") as f:
            f.write(f"{countDownFuck}\n")
    except:
        pass


def writeSnowballData(fileName, item):
    try:
        with open(fileName, "w", encoding="utf-8") as f:
            f.write(f"{item['currentSnowballParentIndex']},{item['currentSnowballChildrenIndex']}\n")
    except:
        pass


def readSnowballData(fileName):
    data = {}
    try:
        with open(fileName, 'r', encoding="utf-8") as file:
            for line in file:
                if line.strip() == "":
                    return ""
                values = line.strip().split(',')
                data['currentSnowballParentIndex'] = values[0]
                data['currentSnowballChildrenIndex'] = values[1]
    except:
        print(f"{fileName} 文件不存在")
        return ""
    return data


def writeUserLoginContext(fileName, item):
    # "model": "1",
    # "winAmount": 1,
    # "costAmount": 1
    with open(fileName, 'w') as file:
        file.write(
            f"{item['Cellphone']},{item['Password']},{item['model']},{item['winAmount']},{item['costAmount']},{item['snowball']},{item['snowballtarget']},{item['isScrpay']}\n")


# with open('lotterInfoItem.txt', 'r') as file:
#     for line in file:
#         values = line.strip().split(',')
#         valuesList = list(values)
#         temp = []
#         temp.append(valuesList[0])
#         temp.append(valuesList[1] + "+" + valuesList[2] + "=" + valuesList[3] + "=" + valuesList[4])
#         temp.append(valuesList[5])
#         temp.append("否")
#         if temp[0] not in id_list:
#             treeview.insert("", "end", values=tuple(temp))

def readUserLoginContext(fileName):
    try:
        data = {}
        with open(fileName, 'r') as file:
            for line in file:
                values = line.strip().split(',')
                data["phone"] = values[0]
                data["password"] = values[1]
                data["model"] = values[2]
                data["winAmount"] = values[3]
                data["costAmount"] = values[4]
                data["snowballCount"] = values[5]
                data["snowballtarget"] = values[6]
                data["isScrpay"] = values[7]

        return data
    except:
        print(f"f{fileName} 不存在")


"""
    更新或删除文件信息
"""


def insertOrUpdateOrDeleteLotterInfoItem(filename, lotterInfo, costSum, WinSum):
    try:
        content = ""
        with open(filename, 'r', encoding="utf-8") as file:
            for line in file:
                values = line.strip().split(',')
                valuesList = list(values)
                if len(valuesList) >= 8:
                    if MumberUtils.strToInt(valuesList[0]) <= MumberUtils.strToInt(lotterInfo):
                        content = getContent(content, valuesList[0:8])
                        if costSum > 0:
                            content += str(costSum) + "," + "是" + "," + str(WinSum) + ","
                        content += "\n"

                    if MumberUtils.strToInt(valuesList[0]) <= MumberUtils.strToInt(
                            lotterInfo) - 1:
                        continue

        if content != "":
            with open(filename, 'w', encoding="utf-8") as file:
                file.write(content)
    except:
        print(f"{filename} 文件不存在")


def getContent(content, valuesList):
    for i in valuesList:
        content += str(i) + ","

    return content


if __name__ == '__main__':
    list = [12, 23, 23, 23]
    for i in list:
        print(i)
